<template>
  <el-dialog
    :title="!dataForm.goodsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="dataForm.goodsName"
          placeholder="商品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品标题" prop="goodsTitle">
        <el-input
          v-model="dataForm.goodsTitle"
          placeholder="商品标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品图片" prop="goodsImg">
        <el-upload
          action="http://localhost:88/api/pms/goods/upload"
          class="upload-demo"
          :on-success="uploadFile"
          :on-remove="handleRemove"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
        <el-input
          type="test"
          v-model="dataForm.goodsImg"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品详细介绍" prop="goodsDetail">
        <el-input
          v-model="dataForm.goodsDetail"
          placeholder="商品详细介绍"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品单价" prop="goodsPrice">
        <el-input
          v-model="dataForm.goodsPrice"
          placeholder="商品单价"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品库存" prop="goodsStock">
        <el-input
          v-model="dataForm.goodsStock"
          placeholder="商品库存"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="info" @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        goodsId: 0,
        goodsName: "",
        goodsTitle: "",
        goodsImg: "",
        goodsDetail: "",
        goodsPrice: "",
        goodsStock: "",
      },
      dataRule: {
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        goodsTitle: [
          { required: true, message: "商品标题不能为空", trigger: "blur" },
        ],
        goodsImg: [
          { required: true, message: "商品图片不能为空", trigger: "blur" },
        ],
        goodsDetail: [
          { required: true, message: "商品详细介绍不能为空", trigger: "blur" },
        ],
        goodsPrice: [
          { required: true, message: "商品单价不能为空", trigger: "blur" },
        ],
        goodsStock: [
          {
            required: true,
            message: "商品库存，-1表示没有限制不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    uploadFile(file) {
      this.dataForm.goodsImg = file.url;
    },
    init(id) {
      this.dataForm.goodsId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.goodsId) {
          this.$http({
            url: this.$http.adornUrl(
              `/pms/goods/info/${this.dataForm.goodsId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.goodsName = data.goods.goodsName;
              this.dataForm.goodsTitle = data.goods.goodsTitle;
              this.dataForm.goodsImg = data.goods.goodsImg;
              this.dataForm.goodsDetail = data.goods.goodsDetail;
              this.dataForm.goodsPrice = data.goods.goodsPrice;
              this.dataForm.goodsStock = data.goods.goodsStock;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/pms/goods/${!this.dataForm.goodsId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              goodsId: this.dataForm.goodsId || undefined,
              goodsName: this.dataForm.goodsName,
              goodsTitle: this.dataForm.goodsTitle,
              goodsImg: this.dataForm.goodsImg,
              goodsDetail: this.dataForm.goodsDetail,
              goodsPrice: this.dataForm.goodsPrice,
              goodsStock: this.dataForm.goodsStock,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
